package com.mvm.web.login;

import com.mvm.store.domain.obj.User;
import com.mvm.store.service.base.AbstractService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * <p>
 * Validate a user login.
 * </p>
 */
public class LoginAction extends ActionSupport implements Preparable {

	private AbstractService<User, Integer> service;
	/*
	 * Field to store User user.
	 */
	private User user;
	private Integer id;

	public void setService(AbstractService<User, Integer> service) {
		this.service = service;
	}

	public void prepare() throws Exception {
		if (id != null)
			user = service.find(id);
	}

	public String execute() throws Exception {
		System.out.println("Validating login");
		if (!user.getUserName().equals("Admin") || !user.getPassword().equals("Admin")) {
			addActionError("Invalid user name or password! Please try again!");
			return Action.ERROR;
		} else {
			return Action.SUCCESS;
		}
	}

	/**
	 * <p>
	 * Provide User user.
	 * </p>
	 * 
	 * @return Returns the User user.
	 */
	public User getUser() {
		return user;
	}

	/**
	 * <p>
	 * Store new User user
	 * </p>
	 * 
	 * @param value
	 *            The user to set.
	 */
	public void setUser(User value) {
		user = value;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
}
